package com.scpii.universal.pns.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.xwork.StringUtils;
import org.springframework.stereotype.Service;

import com.scpii.universal.pns.dao.EcOrderDetailsDao;
import com.scpii.universal.pns.dao.EcOrdersDao;
import com.scpii.universal.pns.dto.EcOrderDetailsDto;
import com.scpii.universal.pns.dto.EcOrderDto;
import com.scpii.universal.pns.dto.EcOrderSearchDto;
import com.scpii.universal.pns.entity.EcOrders;

@Service
public class EcOrdersServiceImpl extends PnsBaseServiceImpl<EcOrders, Long>
		implements EcOrdersService {
	@Resource
	private EcOrdersDao ecOrdersDao;
	@Resource
	private EcOrderDetailsDao ecOrderDetailsDao;

	@Resource
	public void setBaseDao(EcOrdersDao ecOrdersDao) {
		super.setBaseDao(ecOrdersDao);
	}

	@Override
	public List<EcOrderDto> getAll(EcOrderSearchDto condition, Long appId,
			int pageNo, int pageSize) {
		Map<String, Object> argsMap = new HashMap<String, Object>();
		List<EcOrders> orders = ecOrdersDao
				.findByHql(searchHql(condition, appId, argsMap), pageNo,
						pageSize, argsMap);
		EcOrderDto ecOrderDto = null;
		List<EcOrderDto> results = new ArrayList<EcOrderDto>();
		List<EcOrderDetailsDto> detailsDto = null;
		for (EcOrders ecOrders : orders) {
			ecOrderDto = new EcOrderDto();
			ecOrderDto.setEcOrders(ecOrders);
			detailsDto = ecOrderDetailsDao.getAllByProSN(condition,
					ecOrders.getId());
			if (detailsDto == null || detailsDto.size() <= 0)
				continue;
			ecOrderDto.setEcOrderDetailsDto(detailsDto);
			results.add(ecOrderDto);
		}
		return results;
	}

	@Override
	public Integer getAllCount(EcOrderSearchDto condition, Long appId) {
		List<EcOrderDto> orders = this.getAll(condition, appId, 0, 0);
		if (orders != null && orders.size() > 0)
			return orders.size();
		return 0;
	}

	private String searchHql(EcOrderSearchDto condition, Long appId,
			Map<String, Object> argsMap) {
		StringBuffer hql = new StringBuffer(
				"from EcOrders e where e.appId=:appId");
		argsMap.put("appId", appId);
		if (condition.getOrderStatus() >= 0) {
			hql.append(" and e.orderStatus=:orderStatus");
			argsMap.put("orderStatus", condition.getOrderStatus());
		}
		if (condition.getShipStatus() >= 0) {
			hql.append(" and e.shipStatus=:shipStatus");
			argsMap.put("shipStatus", condition.getShipStatus());
		}
		if (condition.getPayStatus() >= 0) {
			hql.append(" and e.payStatus=:payStatus");
			argsMap.put("payStatus", condition.getPayStatus());
		}
		if (!StringUtils.isBlank(condition.getKeyWord())
				&& !"订单编号/收货人/手机号/物流单号".equals(condition.getKeyWord())) {
			hql.append(" and (");
			hql.append("e.orderSn=:keyWord");
			hql.append(" or e.title=:keyWord");
			hql.append(" or e.phoneNumber=:keyWord");
			hql.append(" or e.shipSn=:keyWord");
			hql.append(")");
			argsMap.put("keyWord", condition.getKeyWord());
		}
		hql.append(" order by e.updateDt desc,e.id desc");
		return hql.toString();
	}

	@Override
	public void updateShipInfo(Long id, String shipType, String shipSn) {
		StringBuffer hql = new StringBuffer("update EcOrders e set ");
		hql.append("e.updateDt=:updateDt");
		Map<String, Object> argsMap = new HashMap<String, Object>();
		argsMap.put("updateDt", new Date());// date
		hql.append(",e.shipStatus=:shipStatus");
		argsMap.put("shipStatus", (short) 2);
		hql.append(",e.shipType=:shipType");
		argsMap.put("shipType", shipType);
		hql.append(",e.shipSn=:shipSn");
		argsMap.put("shipSn", shipSn);
		hql.append(" where e.id=:id");
		argsMap.put("id", id);
		ecOrdersDao.hqlOperation(hql.toString(), argsMap);
	}

}
